//
//  MyOrderViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-7.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "MyOrderViewController.h"
#import "MyOrderCell.h"
#import "MyOrderView.h"
#import "OrderDetailViewController.h"
#import "HomeViewController.h"

#define BTN_NEW_ORDER_TAG 1001
#define BTN_DONE_ORDER_TAG 1002

@implementation MyOrderViewController

- (id)init
{
    self = [super initWithNibClass:[MyOrderViewController class]];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"我的订单"];
    
    self.doingOrderView = [self createViewWithType:NewOrder];
}

- (void)leftButtonClick:(id)sender
{
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
    
    HomeViewController *vc = (HomeViewController *)[self findControllerWithClass:[HomeViewController class]];
    if (vc) {
        vc.isToUCenter = YES;
        [self popToViewController:vc];
        [AppDelegate changeToNormalTabBar];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (MyOrderView *)createViewWithType:(OrderType)orderType
{
    CGRect frame = CGRectMake(0, _btnNewOrder.bottom, ScreenWidth, ScreenHeight-_btnNewOrder.bottom);
    MyOrderView *orderView  = [[MyOrderView alloc] initWithFrame:frame andOrderType:orderType];
    orderView.orderVC = self;
    [self.view addSubview:orderView];
    
    return orderView;
}

- (IBAction)selOrderAction:(id)sender
{
    _btnNewOrder.selected = _btnDoneOrder.selected = NO;
    UIButton *button = (UIButton*)sender;
    button.selected = !button.selected;
    
    if (!_doingOrderView) self.doingOrderView = [self createViewWithType:NewOrder];
    if (!_doneOrderView) self.doneOrderView = [self createViewWithType:DoneOrder];
    
    _doingOrderView.hidden = !_btnNewOrder.selected;
    _doneOrderView.hidden = !_btnDoneOrder.selected;
}

@end

